#include "endwidget.h"
#include "ui_endwidget.h"
#include "widget.h"
#include <QMovie>
#include <QMoveEvent>

EndWidget::EndWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::EndWidget)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);
    setWindowIcon(QIcon("C:\\Users\\22509\\Pictures\\logo.jpg"));

    //label显示动态图片
    QMovie *movie=new QMovie();
    movie->setFileName("C:\\Users\\22509\\Pictures\\bg8.gif");
    ui->bg->setMovie(movie);
    movie->start();
    ui->bg->setScaledContents(true);//自适应


    QIcon icon;
    icon.addFile(tr("C:\\Users\\22509\\Pictures\\min.png"));
    ui->MinBtn->setIcon(icon);

    icon.addFile(tr("C:\\Users\\22509\\Pictures\\close.png"));
    ui->CloseBtn->setIcon(icon);

    connect(ui->MinBtn,&QPushButton::clicked,this,&EndWidget::showMinimized);
    connect(ui->CloseBtn,&QPushButton::clicked,this,&EndWidget::close);

}

EndWidget::~EndWidget()
{
    delete ui;
}



void EndWidget::mousePressEvent(QMouseEvent *event)
{
    m_lastPos = event->globalPos();
    isPressedWidget = true; // 当前鼠标按下的即是QWidget而非界面上布局的其它控件
}

void EndWidget::mouseMoveEvent(QMouseEvent *event)
{
    if (isPressedWidget) {
        this->move(this->x() + (event->globalX() - m_lastPos.x()),
                   this->y() + (event->globalY() - m_lastPos.y()));
        m_lastPos = event->globalPos();
    }
}

void EndWidget::mouseReleaseEvent(QMouseEvent *event)
{
    // 其实这里的mouseReleaseEvent函数可以不用重写
    m_lastPos = event->globalPos();
    isPressedWidget = false; // 鼠标松开时，置为false
}


void EndWidget::on_pushButton_2_clicked()
{
    //  清空
//    ui->ListenBtn->setEnabled(true) ;
    participants = 0 ;

    for(int i = 0 ; i <= participants ; i++)
    {
        score[i] = 0 ;
    }
    final_winner = 0 ;
}

void EndWidget::on_pushButton_3_clicked()
{
    for(int i = 1 ; i <= participants ; i++ )
        {
            QString    str = QString("%1号选手的得分为: %2").arg(i).arg(score[i]);
            ui->textEdit->append(str) ;

        }

        int maxn = -1;
        for(int i = 1 ; i <= participants ; i++)
        {
            if(score[i] > maxn)
            {
                  maxn = score[i] ;
                  final_winner = i ;
             }

        }

        for(int i = 1 ; i <= participants ; i++)
        {
            if(score[i] == maxn)
            {
                QString str = QString("最终胜者是：%1号，最终得分为%2").arg(i).arg(maxn) ;
                ui->textEdit->append(str) ;
            }

        }
}
